﻿// JScript 文件

//Ajax提交时, 按钮处于不可用状态.
//很明显, 使用jquery后, 无敌了.
var _hashInputs = {};
var _TransferToEle = "footer";
Sys.Application.add_init(function() {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(function() {
        loading();
    });

    prm.add_endRequest(function() {
        loaded();
    });
});

//设置鼠标移动到表格时, 行背景色变换
Sys.Application.add_load(function() {
    $("table.mouseOver/tbody/tr").each(function() {
        if(!this._haveHover) {
            this._haveHover = true;
            $(this).hover(function(e) {
                this._oldColor = $(this).css("background-color");
                $(this).css("background-color", "#f7f7f7");
                e.stopPropagation();
                e.preventDefault();
                return false;
            }, function() {
                $(this).css("background-color", this._oldColor);
            });
        }
    });
    
    $("table.CampaignPeriodCheckBoxList/tbody/tr/td/span/input").each(function() {
        $(this).focus(function(e) {
            this.select();
        });
    });
    
    $("a.selected").each(function() {
        $(this).parent().parent().addClass("selected");
    });
        
    //将所有Button都样到都统一了
    $("input[@type=submit]").addClass("button");
    $("input[@type=submit]").hover(function() {
        if(this.disabled != "disabled")
            $(this).addClass("buttonHover");
    }, function() {
        $(this).removeClass("buttonHover");
    });
});
	
$( function()
	{
	    $("#aspnetForm").append('<div id="window">' 
	        + '<div id="windowTop">'
	            + '<div id="windowTopContent">Message</div>'
	            + '<img src="../pics/window_min.jpg" id="windowMin" />'
	            + '<img src="../pics/window_max.jpg" id="windowMax" />'
	            + '<img src="../pics/window_close.jpg" id="windowClose" />'
	        + '</div>'
	        + '<div id="windowBottom"><div id="windowBottomContent">&nbsp;</div></div>'
	        + '<div id="windowContent"></div>'
	        + '<img src="../pics/window_resize.gif" id="windowResize" /></div>');
//		$('#windowOpen').bind(
//			'click',
//			function() {
//				if($('#window').css('display') == 'none') {
//					$(this).TransferTo(
//						{
//							to:'window',
//							className:'transferer2', 
//							duration: 400,
//							complete: function()
//							{
//								$('#window').show();
//							}
//						}
//					);
//				}
//				this.blur();
//				return false;
//			}
//		);
		$('#windowClose').bind(
			'click',
			function()
			{
				$('#window').TransferTo(
					{
						to:_TransferToEle,
						className:'transferer2', 
						duration: 300
					}
				).hide();
			}
		);
		$('#windowMin').bind(
			'click',
			function()
			{
				$('#windowContent').SlideToggleUp(300);
				$('#windowBottom, #windowBottomContent').animate({height: 10}, 300);
				$('#window').animate({height:40},300).get(0).isMinimized = true;
				$(this).hide();
				$('#windowResize').hide();
				$('#windowMax').show();
			}
		);
		$('#windowMax').bind(
			'click',
			function()
			{
				var windowSize = $.iUtil.getSize(document.getElementById('windowContent'));
				$('#windowContent').SlideToggleUp(300);
				$('#windowBottom, #windowBottomContent').animate({height: windowSize.hb + 13}, 300);
				$('#window').animate({height:windowSize.hb+43}, 300).get(0).isMinimized = false;
				$(this).hide();
				$('#windowMin, #windowResize').show();
			}
		);
		$('#window').Resizable(
			{
				minWidth: 200,
				minHeight: 60,
				maxWidth: 700,
				maxHeight: 400,
				dragHandle: '#windowTop',
				handlers: {
					se: '#windowResize'
				},
				onResize : function(size, position) {
					$('#windowBottom, #windowBottomContent').css('height', size.height - 33 + 'px');
					var windowContentEl = $('#windowContent').css('width', size.width - 25 + 'px');
					if (!document.getElementById('window').isMinimized) {
						windowContentEl.css('height', size.height - 48 + 'px');
					}
				}
			}
		);
	}
);

//jquery API: http://jquery.org.cn/api/cn/index.xml
//jquery XPath: http://docs.jquery.com/Selectors#Using_CSS_and_XPath_Together

// utils methods

function getScrollLeftAndTop() {
    var scrollLeft = (document.documentElement.scrollLeft ?
            document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ?
            document.documentElement.scrollTop : document.body.scrollTop);   
    return {left:scrollLeft, top:scrollTop};
}
function showErrorMessage(message) {
    showWindow(message, true);
}

function showMessage(message) {
    showWindow(message);
}

var _closeWindowTimer;
function showWindow(message, isError) {
    var html = "<div><img src='../pics/"+(isError?"error":"success")+".png' alt='"+(isError?"错误信息":"成功信息")+".' >"+message+"<p>"
        +"<input id='windowSureButton' type='button' class='windowButton' value='确  定' /></p></div>";
    $('#windowContent').addClass((isError?"error":"success")).html(html);
    $('#windowSureButton').click(function() {
        window.clearTimeout(_closeWindowTimer);
        $('#windowClose').click();
    });
    var scrollLeftTop = getScrollLeftAndTop();
    var clientBounds = getClientBounds();
    var left = parseInt(scrollLeftTop.left + (clientBounds.width - $('#window').width())/2);
    var top = parseInt(scrollLeftTop.top + (clientBounds.height - $('#window').height())/2);
    if(!$('#'+_TransferToEle)[0]) {
        _TransferToEle = "footer";
    }
    if($('#window').css({"left": left, "top": top}).css('display') == 'none') {
		$('#'+_TransferToEle).TransferTo(
			{
				to:'window',
				className:'transferer2', 
				duration: 400,
				complete: function()
				{
					$('#window').show();
					$('#windowSureButton').focus();
				}
			}
		);
	}
	if(!isError) {
	    _closeWindowTimer = window.setTimeout(function() {$('#windowClose').click();}, 1500);
	}
}

function showMessageAndRefresh(message, url) {
    var html = "<div><img src='../pics/success.png' alt='"+message+"' >"+message+"<p>"
        +"<input id='windowSureButton' type='button' class='windowButton' value='确  定' /></p></div>";
    $('#windowContent').addClass("success").html(html);
    $('#windowClose').click(function() { window.location = url; });
    $('#windowSureButton').click(function() {
        window.clearTimeout(_closeWindowTimer);
        $('#windowClose').click();
    });
    var scrollLeftTop = getScrollLeftAndTop();
    var clientBounds = getClientBounds();
    var left = parseInt(scrollLeftTop.left + (clientBounds.width - $('#window').width())/2);
    var top = parseInt(scrollLeftTop.top + (clientBounds.height - $('#window').height())/2);
    if(!$('#'+_TransferToEle)[0]) {
        _TransferToEle = "footer";
    }
    if($('#window').css({"left": left, "top": top}).css('display') == 'none') {
		$('#'+_TransferToEle).TransferTo(
			{
				to:'window',
				className:'transferer2', 
				duration: 400,
				complete: function()
				{
					$('#window').show();
					$('#windowSureButton').focus();
				}
			}
		);
	}
	_closeWindowTimer = window.setTimeout(function() {$('#windowClose').click();}, 1500);
}

function _test(dom) {
    var str ="";
    $.each(dom, function(i,n) {
        str+=i+":"+n+", ";
    });
    alert(str);
}

//删除操作的确定提示
function deleteConfirm(e){
    var oEvent = e;
    if(!oEvent) {
        oEvent = window.event;
    }
    var ele = oEvent.target || oEvent.srcElement; //FF || IE
    if(ele.disabled) return false;
    if(!confirm('确定删除吗？')) {
        if(window.event) {
            oEvent.cancelBubble = true;
            oEvent.returnValue = false;
        } else {
            oEvent.stopPropagation();
            oEvent.preventDefault();
        }
        return false;
    }
}

//获取浏览器可视区域大小
function getClientBounds()
{
    var clientWidth;
    var clientHeight;
    switch(Sys.Browser.agent) {
        case Sys.Browser.InternetExplorer:
            clientWidth = document.documentElement.clientWidth;
            clientHeight = document.documentElement.clientHeight;
            break;
        case Sys.Browser.Safari:
            clientWidth = window.innerWidth;
            clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            clientWidth = Math.min(window.innerWidth, 
                document.body.clientWidth);
            clientHeight = Math.min(window.innerHeight,
                document.body.clientHeight);
            break;
        default:  // Sys.Browser.Firefox, etc.
            clientWidth = Math.min(window.innerWidth,
                document.documentElement.clientWidth);
            clientHeight = Math.min(window.innerHeight,
                document.documentElement.clientHeight);
            break;
    }

    return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
}

function submitConfirm(message) {
    if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == true) {
        return window.confirm(message);
    }
}

//loading message
function loading() {
    if(!$("#loadingMessage").get(0))
    {
        $(document.body).append("<div id='loadingMessage' class='loading'>Loading...</div>");
        $("#loadingMessage").hide();
    }
    
    $("#loadingMessage").show();
    resetLoadingMessengerPosition();
    $(window).scroll(resetLoadingMessengerPosition);
    $(window).resize(resetLoadingMessengerPosition);
}

function loaded() {
    $("#loadingMessage").hide();
    $(window).unbind("scroll");
    $(window).unbind("resize");
}

function resetLoadingMessengerPosition() {
    var clientBounds = getClientBounds();
    var scrollLeft = (document.documentElement.scrollLeft ?
            document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ?
            document.documentElement.scrollTop : document.body.scrollTop);
    var top = scrollTop;
    //滚动条的左边距+窗体内容的一半宽度-元素本身宽度的一半 = 居中
    var left = scrollLeft + parseInt(clientBounds.width/2) - $("#loadingMessage").width() / 2; 
    $("#loadingMessage").css({top: top + "px", left: left + "px"});
}
